var a = require("../../api.js"), aa = a, e = getApp() ,ee = e, s = !1, i = !1;

Page({
    data: {
        swiper_current: 0,
        goods: {
            list: null,
            is_more: !0,
            is_loading: !1,
            page: 1
        },
        topic: {
            list: null,
            is_more: !0,
            is_loading: !1,
            page: 1
        }
    },
    onLoad: function(a) {
        e.pageOnLoad(this, a), this.loadGoodsList({
            reload: !0,
            page: 1
        }), this.loadTopicList({
            reload: !0,
            page: 1
        });
    },
    tabSwitch: function(a) {
        var t = this, o = a.currentTarget.dataset.index;
        t.setData({
            swiper_current: o
        });
    },
    swiperChange: function(a) {
        this.setData({
            swiper_current: a.detail.current
        });
    },
    loadGoodsList: function(o) {
        var i = this;
        i.data.goods.is_loading || o.loadmore && !i.data.goods.is_more || (i.data.goods.is_loading = !0, 
        i.setData({
            goods: i.data.goods
        }), e.request({
            url: a.user.favorite_list,
            data: {
                page: o.page
            },
            success: function(a) {
                0 == a.code && (o.reload && (i.data.goods.list = a.data.list), o.loadmore && (i.data.goods.list = i.data.goods.list.concat(a.data.list)), 
                i.data.goods.page = o.page, i.data.goods.is_more = a.data.list.length > 0, i.setData({
                    goods: i.data.goods
                }));
            },
            complete: function() {
                i.data.goods.is_loading = !1, i.setData({
                    goods: i.data.goods
                });
            }
        }));
    },
    goodsScrollBottom: function() {
        var a = this;
        a.loadGoodsList({
            loadmore: !0,
            page: a.data.goods.page + 1
        });
    },
    loadTopicList: function(o) {
        var i = this;
        i.data.topic.is_loading || o.loadmore && !i.data.topic.is_more || (i.data.topic.is_loading = !0, 
        i.setData({
            topic: i.data.topic
        }), e.request({
            url: a.user.topic_favorite_list,
            data: {
                page: o.page
            },
            success: function(a) {
                0 == a.code && (o.reload && (i.data.topic.list = a.data.list), o.loadmore && (i.data.topic.list = i.data.topic.list.concat(a.data.list)), 
                i.data.topic.page = o.page, i.data.topic.is_more = a.data.list.length > 0, i.setData({
                    topic: i.data.topic
                }));
            },
            complete: function() {
                i.data.topic.is_loading = !1, i.setData({
                    topic: i.data.topic
                });
            }
        }));
    },
    topicScrollBottom: function() {
        var a = this;
        a.loadTopicList({
            loadmore: !0,
            page: a.data.topic.page + 1
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onReachBottom: function() {
        this.loadMoreGoodsList();
    },
  get_goods_info: function (t) {
    var a = this,
      o = a.data.carGoods,
      r = a.data.total,
      i = a.data.quick_hot_goods_lists,
      s = a.data.quick_list,
      e = {
        carGoods: o,
        total: r,
        quick_hot_goods_lists: i,
        check_num: a.data.check_num,
        quick_list: s
      };
    wx.setStorageSync("item", e);
    var d = t.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/goods/goods?id=" + d
    });
  },
  jia: function (t) {
    if (!wx.getStorageSync('user_info') || wx.getStorageSync('user_info').length == 0) {
      wx.showToast({
        title: '请先去用户中心登录',
        icon: 'none'
      });
      return false;
    }
    var that = this,
      o = t.currentTarget.dataset,
      r = that.data.goods.list;

    var goods_index = t.currentTarget.dataset.index;
    var goods_id = t.currentTarget.dataset.id;
    var goods_cid = t.currentTarget.dataset.cid;
    for (var s in r)
      if (parseInt(r[s].id) === parseInt(goods_id)) {
        var ee = r[s].num ? r[s].num + 1 : 1;
        //  if (ee > JSON.parse(r[s].attr)[0].num) return void wx.showToast({
        //    title: "商品库存不足",
        //    image: "/images/icon-warning.png"
        //  });
        if (r[s].id == goods_id) {
          var ii = i,
            ss = s;
          wx.showLoading({
            title: "加载中"
          })
          var attr = JSON.parse(r[s].attr)[0].attr_list[0];
          e.request({
            url: a.cart.add_cart,
            method: "POST",
            data: {
              goods_id: parseInt(r[s].id),
              attr: '[{"attr_group_id":1,"attr_group_name":"规格","attr_id":' + attr.attr_id + ',"attr_name":"' + attr.attr_name + '"}]',
              num: 1
            },
            success: function (t) {
              wx.hideLoading();
              if (t.code == 0) {
                r[ss].c_num++;
                that.setData({
                  goods: {
                    list: r
                  }
                });
              } else {
                wx.showToast({
                  icon: 'none',
                  title: t.msg,
                  duration: 1500
                })
              }
              that.cart_num();
            },
            complete: function (res) {
              wx.hideLoading()
            }
          })
        }
      }
  },
  jian: function (t) {
    var that = this,
      o = t.currentTarget.dataset,
      r = that.data.goods.list;
    var goods_index = t.currentTarget.dataset.index;
    var goods_id = t.currentTarget.dataset.id;
    var goods_cid = t.currentTarget.dataset.cid;

    for (var s in r)
      if (r[s].id == goods_id) {
        if (r[s].id == goods_id) {
          var ii = i,
            ss = s;
          wx.showLoading({
            title: "加载中"
          })
          e.request({
            url: a.default.cart_reduce,
            data: {
              goods_id: parseInt(r[s].id),
            },
            success: function (t) {
              if (t.code == 0) {
                r[ss].c_num--;
                that.setData({
                  goods:{
                    list: r
                  }
                });
              } else {
                wx.showToast({
                  icon: 'none',
                  title: t.msg,
                  duration: 1000
                })
              }
              that.cart_num();
            },
            complete: function () {
              wx.hideLoading();
            }
          })
        }
      }
  },
  showDialogBtn: function (o) {
    var r = this,
      i = o.currentTarget.dataset,
      goods_id = o.currentTarget.dataset.id;
    console.log(goods_id);
    wx.showLoading({
      title: "加载中"
    })
    e.request({
      url: a.default.goods,
      data: {
        id: goods_id
      },
      success: function (t) {
        wx.hideLoading()
        0 == t.code && (r.setData({
          currentGood: t.data,
          goods_name: t.data.name,
          attr_group_list: t.data.attr_group_list,
          showModal: !0
        }), r.resetData(), r.updateData());
      },
      complete: function () {
        wx.hideLoading()
      }
    });
  },
  attrClick: function (t) {
    var a = this,
      o = t.target.dataset.groupId,
      r = t.target.dataset.id,
      i = a.data.attr_group_list,
      s = a.data.currentGood;
    for (var e in i)
      if (i[e].attr_group_id == o)
        for (var d in i[e].attr_list) i[e].attr_list[d].attr_id == r ? i[e].attr_list[d].checked = !0 : i[e].attr_list[d].checked = !1;
    var c = [];
    for (var e in i)
      for (var d in i[e].attr_list) !0 === i[e].attr_list[d].checked && c.push([i[e].attr_list[d].attr_id, s.id]);
    var n = JSON.parse(a.data.currentGood.attr),
      u = a.data.temporaryGood;
    for (var _ in n) {
      var g = [];
      for (var l in n[_].attr_list) g.push([n[_].attr_list[l].attr_id, s.id]);
      if (g.sort().join() === c.sort().join()) {
        if (0 === parseInt(n[_].num)) return void wx.showToast({
          title: "商品库存不足",
          image: "/images/icon-warning.png"
        });
        u = parseFloat(n[_].price) ? {
          price: n[_].price.toFixed(2)
        } : {
            price: s.price.toFixed(2)
          };
      }
    }
    a.resetData(), a.checkUpdateData(c), a.setData({
      attr_group_list: i,
      temporaryGood: u,
      checked_attr: c
    });
  },
  // 获取购物车数量
  cart_num: function () {
    var that = this;
    wx.getStorage({
      key: 'user_info',
      success: function (res) {
        e.request({
          url: a.cart.list,
          success: function (t) {
            if (0 == t.code) {
              if (typeof that.getTabBar === 'function' &&
                that.getTabBar()) {
                if (t.data.mch_list.length > 0) {
                  var length = t.data.list.length + t.data.mch_list[0].list.length;
                } else {
                  var length = t.data.list.length
                }
                that.getTabBar().setData({
                  cart_num: length
                })
              }
            }
          }
        });
      },
    })
  },
  resetData: function () {
    this.setData({
      checked_attr: [],
      check_num: 0,
      check_goods_price: 0,
      temporaryGood: {
        price: "0.00"
      }
    });
  },
  checkUpdateData: function (t) {
    var a = this,
      o = a.data.carGoods;
    for (var r in o) {
      var i = [];
      for (var s in o[r].attr) i.push([o[r].attr[s].attr_id, o[r].goods_id]);
      i.sort().join() === t.sort().join() && a.setData({
        check_num: o[r].num,
        check_goods_price: o[r].goods_price
      });
    }
  },
  updateData: function () {
    var t = this,
      a = t.data.currentGood,
      o = t.data.carGoods,
      r = JSON.parse(a.attr),
      i = a.attr_group_list;
    for (var s in r) {
      var e = [];
      for (var d in r[s].attr_list) e.push([r[s].attr_list[d].attr_id, a.id]);
      for (var c in o) {
        var n = [];
        for (var u in o[c].attr) n.push([o[c].attr[u].attr_id, o[c].goods_id]);
        if (e.sort().join() === n.sort().join()) {
          for (var _ in i)
            for (var g in i[_].attr_list)
              for (var l in e) {
                if (parseInt(i[_].attr_list[g].attr_id) === parseInt(e[l])) {
                  i[_].attr_list[g].checked = !0;
                  break;
                }
                i[_].attr_list[g].checked = !1;
              }
          var h = {
            price: o[c].price
          };
          return void t.setData({
            attr_group_list: i,
            check_num: o[c].num,
            check_goods_price: o[c].goods_price,
            checked_attr: e,
            temporaryGood: h
          });
        }
      }
    }
  },
  hideGoodsModel: function () {
    this.setData({
      showModal: !1
    });
  },
  close_box: function (t) {
    this.setData({
      showModal: !1
    });
  },
  onConfirm: function (t) {
    var a = this,
      o = a.data.attr_group_list,
      r = a.data.checked_attr,
      i = a.data.currentGood;
    if (r.length === o.length) {
      var s = a.data.check_num ? a.data.check_num + 1 : 1,
        e = JSON.parse(i.attr);
      for (var d in e) {
        var c = [];
        for (var n in e[d].attr_list)
          if (c.push([e[d].attr_list[n].attr_id, i.id]), c.sort().join() === r.sort().join()) {
            var u = e[d].price ? e[d].price : i.price,
              _ = e[d].attr_list;
            if (s > e[d].num) return void wx.showToast({
              title: "商品库存不足",
              image: "/images/icon-warning.png"
            });
          }
      }

      for (var ii in o) {
        for (var kk in o[ii].attr_list) {
          if (o[ii].attr_list[kk].checked) {
            o[ii].attr_id = o[ii].attr_list[kk].attr_id;
            o[ii].attr_name = o[ii].attr_list[kk].attr_name;
          }
        }
        delete o[ii].attr_list;
      }
      wx.showLoading({
        title: '加载中',
      })
      ee.request({
        url: aa.cart.add_cart,
        method: "POST",
        data: {
          goods_id: i.id,
          attr: JSON.stringify(o),
          num: 1
        },
        success: function (t) {
          if (t.code == 0) {
            wx.showToast({
              title: t.msg,
              duration: 1500
            });
            a.cart_num()
          }
        },
        complete: function () {
          wx.hideLoading()
        }
      })

      var g = a.data.carGoods,
        l = 1,
        h = (parseFloat(u) * s).toFixed(2);
      for (var p in g) {
        var f = [];
        for (var v in g[p].attr) f.push([g[p].attr[v].attr_id, g[p].goods_id]);
        if (f.sort().join() === r.sort().join()) {
          l = 0, g[p].num = g[p].num + 1, g[p].goods_price = (parseFloat(u) * g[p].num).toFixed(2);
          break;
        }
      }
      a.hideGoodsModel();
    } else wx.showToast({
      title: "请选择规格",
      image: "/images/icon-warning.png"
    });
  },
  deleteShixiaoGoods: function (e) {
    var that = this;
    var goods_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确认删除改失效商品吗？",
      success: function (e) {
        e.confirm && (wx.showLoading({
          title: "正在删除",
          mask: !0
        }), ee.request({
          url: a.user.favorite_remove,
          method: "post",
          data: {
            goods_id: goods_id
          },
          success: function (s) {
            wx.hideLoading();
            if (0 == s.code){
              that.loadGoodsList({
                reload: !0,
                page: 1
              })
            }else{
              wx.showToast({
                title: s.msg,
                image: "/images/icon-warning.png"
              })
            }
    
          }
        }));
      }
    });
  }
});